from flask import Blueprint,render_template,g,redirect,request,jsonify
from app import db
from app.models import Product,User,Acctproduct,Updateturn,Updatejour,Bopupdateturn
import os
import time,random

turn_page = Blueprint('turn_page',__name__,template_folder='templates')
@turn_page.route('/turn',methods = ['GET','POST'])
def turn():
    if g.user is not None and g.user.is_authenticated:
        hs_current_id = str(g.user).replace("'", "")
        hs_current_user = db.session.query(User.username).filter_by(id = hs_current_id).first()[0]
        mes_lists = db.session.query(Updatejour.output,Updatejour.update_date,Updatejour.update_time,Updatejour.message,Updatejour.id,Updatejour.log_path).filter_by(read_status = 1,up_persion=hs_current_user).all()
        mes_len = len(mes_lists)
        turn_lists = db.session.query(Updateturn.id,Updateturn.up_host,Updateturn.update_way,Updateturn.output,Updateturn.begin_date,Updateturn.up_persion).filter_by(up_status=1).all()
        bop_turn_lists = db.session.query(Bopupdateturn.id,Bopupdateturn.bop_ip,Bopupdateturn.bop_path,Bopupdateturn.bop_war,Bopupdateturn.up_persion).filter_by(up_status=1).all()
        if request.method == 'POST':
            if request.values.get('fun')=='deletemes':
                de_id = request.values.get('mes_id')
                u = Updatejour.query.filter_by(id = de_id).first()
                u.read_status = 2
                db.session.add(u)
                db.session.commit()
            if request.values.get('fun') == 'change':
                get_id = request.values.get('table_id')
                get_change = db.session.query(Updateturn.progress).filter_by(id=int(get_id)).all()
                out_change = get_change[0][0]
                a = out_change.split(',')[0]
                b = out_change.split(',')[1]
                data = {'value':int(a),'progress':b}
                return jsonify(data)
    else:
        return redirect('/login')
    return render_template('hs-turn.html',hs_current_user=hs_current_user,bop_turn_lists=bop_turn_lists,turn_lists=turn_lists,mes_lists=mes_lists,mes_len=mes_len)